package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Community;
import com.atguigu.entity.Dict;
import com.atguigu.service.CommunityService;
import com.atguigu.service.DictService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author lhh
 * @ClassName CommunityController
 * @date 2022/9/17 15:20
 */

@Controller
@RequestMapping("community")
public class CommunityController extends BaseController {

    @Reference
    private CommunityService communityService;

    @Reference
    private DictService dictService;

    @RequestMapping
    public String index(HttpServletRequest request, Model model){
        Map<String, Object> filters = getFilters(request);

        PageInfo<Community> page = communityService.findPage(filters);

        List<Dict> areaList = dictService.findListByDictCode("beijing");
        model.addAttribute("areaList",areaList);
        model.addAttribute("filters",filters);
        model.addAttribute("page",page);

        return "community/index";
    }


    @GetMapping("create")
    public String create(Model model){
        List<Dict> areaList = dictService.findListByDictCode("beijing");
        model.addAttribute("areaList",areaList);
        return "community/create";
    }
    
    @PostMapping("save")
    public String save(Community community){
        communityService.save(community);
        return "common/successPage";
    }

    @GetMapping("edit/{id}")
    public String getByIdComm(@PathVariable Long id,Model model){
        Community community = communityService.getById(id);
        model.addAttribute("community",community);
        List<Dict> areaList = dictService.findListByDictCode("beijing");
        model.addAttribute("areaList",areaList);
        return "community/edit";
    }

    @PostMapping("update")
    public String update(Community community){
        communityService.update(community);
        return "common/successPage";
    }

    @GetMapping("/delete/{id}")
    public String del(@PathVariable Long id){
        communityService.remove(id);
        return "redirect:/community";
    }
}
